package cn.edu.cqu.bysj.wm.gui;
import javax.imageio.ImageIO;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.io.*;
import java.net.URL;
import java.awt.*;

import javax.swing.*;
import javax.swing.tree.TreeCellRenderer;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.tree.TreePath;
/**
 * @author copy from 蒋家狂潮
 */

public class SystemFileTree{
	protected DefaultTreeModel model;
	protected JTree tree;
	protected JScrollPane scroll;
	
	protected String selectedFilePath;
	
	/**
	 * a component
	 */
	SystemFileTree (String filePath){
		tree=new JTree();
		tree.addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e){
				node_mouseAction(e);
			}
		});
		 scroll=new JScrollPane(tree);
		this.initData(filePath);
	}
	public Component getComponent(){
		return scroll;
	}
	/**
	 * a Tree with a Frame.
	 */
	SystemFileTree () {
		JFrame f=new JFrame();
		f.getContentPane().setLayout(new BorderLayout());
		tree=new JTree();
		tree.addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e){
				node_mouseAction(e);
			}
		});
		JScrollPane scroll=new JScrollPane(tree);
		f.getContentPane().add(scroll,BorderLayout.CENTER);
		f.setLocation(250,250);
		f.setSize(new Dimension(300,500));
		f.setVisible(true);
	}
	protected void node_mouseAction(MouseEvent e){
		int row = tree.getRowForLocation(e.getX(), e.getY());
		PathNode pathNode =null;
		if(row != -1){
			TreePath path = tree.getPathForRow(row);
			pathNode = (PathNode)path.getLastPathComponent();
			if(pathNode.isFolder()&&pathNode.getChildCount()==0){
				builderNode(pathNode);
				tree.expandPath(path);
			}
			String temp=(String) pathNode.getValue();
			//this.selectedFilePath=temp.substring(temp.lastIndexOf(File.separator)+1);
			this.selectedFilePath=temp;
		}
		
	}
	private PathNode builderNode(PathNode pathNode){
		String filePath= pathNode.getValue().toString();
		File file=new File(filePath);
		File[] files=file.listFiles();
		for(int i=0;i<files.length;i++){
			PathNode node=new PathNode(files[i].getName(), files[i].getAbsolutePath(),files[i].isDirectory());
			pathNode.add(node);
		}
		return pathNode;
	}
	public void initData(String  rootPath){
		File f=new File(rootPath);
		PathNode root=new PathNode(f.getName(), rootPath,f.isDirectory());
		File[] files=f.listFiles();
		for(int i=0;i<files.length;i++){
			PathNode node=new PathNode(files[i].getName(), files[i].getAbsolutePath(),files[i].isDirectory());
			root.add(node);
		}
		model=new DefaultTreeModel(root);
		tree.setModel(model);
		FileTreeRenderer renderer=new FileTreeRenderer();
		tree.setCellRenderer(renderer);

		tree.repaint();
	}
	
	private static  Icon folder_open;//=new ImageIcon(("resources/icons/folder_open.png"));
	private static Icon folder_close;//=new ImageIcon(("resources/icons/folder_close.png"));
	private static Icon file;//=new ImageIcon(("resources/icons/file.png"));
	static{
		try {
			URL imageURL = ImageButton.class.getResource("/folder_open.png");
			Image image=ImageIO.read(imageURL).getScaledInstance(30, 30,  Image.SCALE_SMOOTH);
			folder_open=new ImageIcon(image);
			
			imageURL = ImageButton.class.getResource("/folder_close.png");
			 image=ImageIO.read(imageURL).getScaledInstance(30, 30,  Image.SCALE_SMOOTH);
			folder_close=new ImageIcon(image);
			
			imageURL = ImageButton.class.getResource("/file.png");
			 image=ImageIO.read(imageURL).getScaledInstance(30, 30,  Image.SCALE_SMOOTH);
			file=new ImageIcon(image);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	class FileTreeRenderer implements TreeCellRenderer{
		

		public Component getTreeCellRendererComponent(JTree tree, Object value,
				boolean selected, boolean expanded, boolean leaf, int row,
				boolean hasFocus) {
			JLabel label = null;
			try{
				if (value != null) {
					//System.out.println(value.getClass().toString());
					if(value instanceof PathNode){
						PathNode pathNode = (PathNode) value;
						if (pathNode.isFolder()) {
							if (expanded) {
								label = new JLabel(pathNode.getUserObject().
										toString(),
										folder_open, JLabel.RIGHT);
							} else if(!expanded||leaf) {
								label = new JLabel(pathNode.getUserObject().
										toString(),
										folder_close, JLabel.RIGHT);
							}

						} else {
							label = new JLabel(pathNode.getUserObject().toString(),
									file, JLabel.RIGHT);
						}
						
						return label;
					}
				}
			}catch (Throwable e) {
				e.printStackTrace();// TODO: handle exception
			}
			return label;
		}
	}
	class PathNode extends DefaultMutableTreeNode{
		Object value;
		boolean isFolder;
		public PathNode(String name,Object value,boolean isFolder){
			super(name);
			this.value=value;
			this.isFolder=isFolder;
		}
		public Object getValue(){
			return value;
		}
		public boolean isFolder(){
			return isFolder;
		}
	}
	public static void main(String args[]) {
		//JDHSystemFileTree tree = new JDHSystemFileTree();
		SystemFileTree tree = new SystemFileTree();
		tree.initData("D:/");
	}
	public String getSelectedFilePath() {
		return selectedFilePath;
	}
	
}